package com.tiancheng.trade.payment.controller.api;

import com.tiancheng.trade.commom.core.data.Result;
import com.tiancheng.trade.commom.core.utils.JsonUtil;
import com.tiancheng.trade.commom.web.model.request.OrderPayDTO;
import com.tiancheng.trade.commom.web.model.request.RefundVerificationRequestVO;
import com.tiancheng.trade.commom.web.model.request.VerificationRequestVO;
import com.tiancheng.trade.commom.web.model.response.UnifiedOrderResVO;
import com.tiancheng.trade.commom.web.model.response.VerificationOrderResponseVO;
import com.tiancheng.trade.payment.channel.bo.GetUnifiedOrderResBO;
import com.tiancheng.trade.payment.service.PaymentOrderService;
import com.tiancheng.trade.payment.service.PaymentRefundService;
import com.tiancheng.trade.payment.vo.api.*;
import com.tiancheng.trade.commom.web.model.response.RefundOrderResVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import jakarta.validation.Valid;

/**
 * 渠道下单、退款等
 */
@RestController
@RequestMapping("/api/order")
@Validated
@Slf4j
public class PaymentOrderApiController {

	@Resource
	private PaymentOrderService paymentOrderService;
	@Resource
	private PaymentRefundService paymentRefundService;

	/**
	 * 统一下单
	 * @param unifiedOrderVO
	 * @return
	 */
	@PostMapping("/unified")
	public Result<UnifiedOrderResVO> unified(@RequestBody @Valid OrderPayDTO unifiedOrderVO){
	    log.info("统一支付下单数据:{}", JsonUtil.toJsonString(unifiedOrderVO));
	    return Result.success(paymentOrderService.unifiedOrder(unifiedOrderVO));
	}

	/**
	 * 统一下单——抵扣（免支付）
	 * @param unifiedOrderVO
	 * @return
	 */
	@PostMapping("/deductionUnified")
	public Result deductionUnified(@RequestBody @Valid OrderPayDTO unifiedOrderVO){
		log.info("抵扣支付下单数据:{}", JsonUtil.toJsonString(unifiedOrderVO));
		return Result.success(paymentOrderService.deductionUnified(unifiedOrderVO));
	}

	/**
	 * 苹果支付
	 * @param unifiedOrderVO
	 * @return
	 */
	@PostMapping(value = "/appleInAppPay", consumes = MediaType.APPLICATION_JSON_VALUE)
	public Result appleInAppPay(@RequestBody @Valid OrderPayDTO unifiedOrderVO) {
		return Result.success(paymentOrderService.appleInAppPay(unifiedOrderVO));
	}

	/**
	 * 苹果支付
	 * @param unifiedOrderVO
	 * @return
	 */
	@PostMapping(value = "/appleInAppPay/v2", consumes = MediaType.APPLICATION_JSON_VALUE)
	public Result appleInAppPayV2(@RequestBody @Valid OrderPayDTO unifiedOrderVO) {
		return Result.success(paymentOrderService.appleInAppPayV2(unifiedOrderVO));
	}


	/**
	 * 退款
	 * @param refundOrderVO
	 * @return
	 */
	@PostMapping("/refund")
	public Result<RefundOrderResVO> refund(@RequestBody @Valid RefundOrderVO refundOrderVO) {
		log.info("退款数据:{}", JsonUtil.toJsonString(refundOrderVO));
		return Result.success(paymentRefundService.refund(refundOrderVO));
	}


	/**
	 * 抵扣支付——退款（免支付）
	 * @param refundOrderVO
	 * @return
	 */
	@PostMapping("/deductionRefund")
	public Result deductionRefund(@RequestBody @Valid RefundOrderVO refundOrderVO){
		log.info("抵扣支付退款数据:{}", JsonUtil.toJsonString(refundOrderVO));
		return Result.success(paymentRefundService.deductionRefund(refundOrderVO));
	}

	/**
	 * 关闭订单
 	 * @param closeOrderVO
	 * @return
	 */
	@PostMapping("/close")
	public Result<String> closeOrder(@RequestBody @Valid CloseOrderVO closeOrderVO) {
		log.info("关闭订单:{}", JsonUtil.toJsonString(closeOrderVO));
		return paymentOrderService.closeOrder(closeOrderVO);
	}

	/**
	 * 核销订单
 	 * @param verificationRequestVO
	 * @return
	 */
	@PostMapping("/verification")
	public Result<VerificationOrderResponseVO> verification(@RequestBody @Valid VerificationRequestVO verificationRequestVO) {
		log.info("调用核销接口 data:{}", JsonUtil.toJsonString(verificationRequestVO));
		return paymentOrderService.verification(verificationRequestVO);
	}

	/**
	 * 核销订单
	 * @param refundVerificationRequestVO
	 * @return
	 */
	@PostMapping("/refundVerification")
	public Result refundVerification(@RequestBody @Valid RefundVerificationRequestVO refundVerificationRequestVO) {
		log.info("调用退款核销接口 data:{}", JsonUtil.toJsonString(refundVerificationRequestVO));
		paymentRefundService.verification(refundVerificationRequestVO);
		return Result.success();
	}


	/**
	 * 查询支付结果
	 * @param getUnifiedOrderVo
	 * @return
	 */
	@PostMapping("/queryPayResult")
	public Result<GetUnifiedOrderResBO> queryPayResult(@RequestBody @Valid GetUnifiedOrderVo getUnifiedOrderVo) {
		log.info("查询计平支付结果接口 data:{}", JsonUtil.toJsonString(getUnifiedOrderVo));
		return paymentOrderService.queryPayResult(getUnifiedOrderVo);
	}
}
